﻿Public Class frmReserveRoomBE

    Private da As New DSVaultingTableAdapters.rr_reserve_roomsTableAdapter
    Private action As String
    Private actType As String() = {"NONE", "INSERT", "UPDATE", "DELETE"}
    Private oldId As Long

    Private Sub frmReserveRoomBE_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        'TODO: This line of code loads data into the 'DSVaulting.rr_reserve_room_branches' table. You can move, or remove it, as needed.
        Me.Rr_reserve_room_branchesTableAdapter.Fill(Me.DSVaulting.rr_reserve_room_branches)
        uxRoomDgv.DataSource = da.GetDataWithLocation
    End Sub

    Private Sub frmReserveRoomBE_FormClosed(ByVal sender As System.Object, ByVal e As System.Windows.Forms.FormClosedEventArgs) Handles MyBase.FormClosed
        frmSetting.Enabled = True
    End Sub

    Private Sub updateDGV()
        uxRoomDgv.DataSource = da.GetDataWithLocation
    End Sub

    Private Sub uxNewBranchBtn_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles uxNewBranchBtn.Click
        uxRoomNameTbx.Enabled = True
        uxRoomNameTbx.Clear()
        action = actType(1)
    End Sub

    Private Sub uxSaveBtn_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles uxSaveBtn.Click
        Dim val As String = uxRoomNameTbx.Text
        Dim sample As String = If(uxIsSampleChx.Checked, 1, 0)
        If Not val = "" Then
            If action = actType(1) Then
                da.Insert(uxRoomTypeCbx.SelectedValue, val, sample)
                uxRoomNameTbx.Clear()
                updateDGV()
            ElseIf action = actType(2) Then
                If MessageBox.Show("តើអ្នកពិតជាចងរក្សាទុកព័ត៌មានដែលបានកែប្រែមែនទេ?", "សារបញ្ជាក់ពីការកែប្រែ", MessageBoxButtons.YesNo, MessageBoxIcon.Question, MessageBoxDefaultButton.Button1) = Windows.Forms.DialogResult.Yes Then
                    da.Update(uxRoomTypeCbx.SelectedValue, val, sample, oldId)
                    updateDGV()
                End If
            End If
        End If
    End Sub

    Private Sub uxEditBranchBtn_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles uxEditBranchBtn.Click
        If uxRoomDgv.SelectedRows.Count > 0 Then
            uxRoomNameTbx.Enabled = True
            uxRoomNameTbx.Text = uxRoomDgv.SelectedRows.Item(0).Cells("RoomName").Value
            If Not uxRoomNameTbx.Text = "" Then
                oldId = uxRoomDgv.SelectedRows.Item(0).Cells("Index").Value
                action = actType(2)
            End If
        End If
    End Sub

    Private Sub uxCancelBtn_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles uxCancelBtn.Click
        frmSetting.Enabled = True
        Me.Close()
    End Sub

    Private Sub uxDeleteBranchBtn_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles uxDeleteBranchBtn.Click
        If MessageBox.Show("តើអ្នកពិតជាចង់លុបចោលទិន្នន័យនេះមែនទេ?", "សារបញ្ជាក់ពីការលុបចោល", MessageBoxButtons.YesNo, MessageBoxIcon.Question, MessageBoxDefaultButton.Button1) = Windows.Forms.DialogResult.Yes Then
            Dim oldId As Long = uxRoomDgv.SelectedRows.Item(0).Cells(0).Value
            da.Delete(uxRoomDgv.SelectedRows.Item(0).Cells(0).Value)
            action = actType(3)
            Me.updateDGV()
        End If
    End Sub

    Private Sub uxRoomDgv_CellDoubleClick(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles uxRoomDgv.CellDoubleClick
        uxEditBranchBtn_Click(sender, e)
    End Sub
End Class